extern crate sflag;

fn main() {
    let mut create = false;
    let mut extract = false;
    let mut list = false;
    let mut archive_file: Option<String> = None;

    let mut gzip = false;
    let mut bzip2 = false;

    let mut strip_comp: u32 = 0;
    let mut help = false;

    let mut fs = sflag::FlagSet::new();

    fs.flag("help", Some('h'), &mut help, "Print this help and exit")
        .flag(
            "create",
            Some('c'),
            &mut create,
            "Create a new archive.
Arguments supply the names of the files, to be archived. Directories are
archived recursively, unless the `--no-recursion' option is given.",
        )
        .flag(
            "list",
            Some('t'),
            &mut list,
            "List the contents of an archive.
Arguments are optional. When given, they specify the names of the members to
list.",
        )
        .flag(
            "extract",
            Some('x'),
            &mut extract,
            "List the contents of an archive.
Arguments are optional. When given, they specify the names of the members to
list.",
        )
        .flag(
            "file",
            Some('f'),
            &mut archive_file,
            "Path to a archive file",
        )
        .flag(
            "gzip",
            Some('z'),
            &mut gzip,
            "Filter the archive through gzip(1).",
        )
        .flag(
            "bzip2",
            Some('j'),
            &mut bzip2,
            "Filter the archive through bzip2(1)",
        )
        .flag(
            "strip-components",
            None,
            &mut strip_comp,
            "Strip some leading components from file names on extraction.",
        );

    let r = fs.parse(&std::env::args().collect::<Vec<_>>()).unwrap();

    if help {
        r.write_usage(&mut std::io::stderr()).unwrap();
        std::process::exit(0);
    }

    if [extract, list, create].into_iter().filter(|x| *x).count() != 1 {
        eprintln!(
            "tar: You must specify one of the '-ctx' options
Try 'tar -h' or 'tar --help' for more information."
        );
        std::process::exit(2);
    }

    let Some(archive_file) = archive_file else {
        eprintln!("tar: missing -f,--file");
        std::process::exit(2);
    };

    println!("gzip            : {}", gzip);
    println!("bzip2           : {}", bzip2);
    println!("strip_components: {}", strip_comp);
    println!("archive_file    : {:?}", archive_file);
    println!("file            : {:?}", r.positionals);
}
